package services

import (
	"go_demo/internal/dao"
	"go_demo/internal/models"

	"golang.org/x/crypto/bcrypt"
)

// 定义UserService结构体，返回UserDao
type UserService struct {
	userDao *dao.UserDao
}

// 定义实例化UserService的方法
func NewUserService(userDao *dao.UserDao) *UserService {
	return &UserService{userDao: userDao}
}

// 创建用户
func (us *UserService) CreateUser(user *models.User) error {
	// Hash the password before saving to the database
	hashedPassword, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
	if err != nil {
		return err
	}
	user.Password = string(hashedPassword)

	return us.userDao.CreateUser(user)
}

// 通过id查找用户
func (us *UserService) GetUserByID(id uint) (*models.User, error) {
	return us.userDao.GetUserByID(id)
}

// 查找所有用户
func (us *UserService) GetAllUser() ([]models.User, error) {
	return us.userDao.GetAllUser()
}

// 通过id删除用户
func (us *UserService) DeleteUserById(id int) error {
	return us.userDao.DeleteUserById(id)
}

// 更新用户
func (us *UserService) UpdateUser(id uint, user *models.User) error {
	return us.userDao.UpdateUser(id, user)
}

// 用户登录
func (us *UserService) UserLogin(name string) (*models.User, error) {
	return us.userDao.UserLogin(name)
}
